package com.atguigu.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.base.BaseDao;
import com.atguigu.base.BaseServiceImpl;
import com.atguigu.dao.UserFollowDao;
import com.atguigu.entity.UserFollow;
import com.atguigu.service.DictService;
import com.atguigu.service.UserFollowService;
import com.atguigu.service.UserInfoService;
import com.atguigu.vo.UserFollowVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service(interfaceClass = UserFollowService.class)
@Transactional
public class UserFollowServiceImpl extends BaseServiceImpl<UserFollow> implements UserFollowService {
    @Autowired
    private UserFollowDao userFollowDao;

    @Reference    //因为dictservice是另一台服务器提供的,虽然这里也是提供者,但也可以作为一个消费者,这里得用reference
    private DictService dictService;

    @Override
    protected BaseDao<UserFollow> getEntityDao() {
        return userFollowDao;
    }

    @Override
    public boolean isFollow(Long houseId, Long userId) {
        int rows = userFollowDao.isFollow(houseId,userId);
        if (rows>0){
            return true;
        }
        return false;
    }

    @Override
    public PageInfo<UserFollowVo> findListPage(int pageNum, int pageSize, Long userId) {
        //分页查询
        PageHelper.startPage(pageNum,pageSize);
        Page<UserFollowVo> page = userFollowDao.findListPage(userId);
        //补全前端所需的vo对象里的字段
        List<UserFollowVo> userFollowVoList = page.getResult();
        for (UserFollowVo userFollowVo : userFollowVoList) {
            String houseTypeName =dictService.getNameById(userFollowVo.getHouseTypeId());
            userFollowVo.setHouseTypeName(houseTypeName);
            String floorName =dictService.getNameById(userFollowVo.getFloorId());
            userFollowVo.setFloorName(floorName);
            String directionName =dictService.getNameById(userFollowVo.getDirectionId());
            userFollowVo.setDirectionName(directionName);
        }
        //封装成pageInfo返回
        return new PageInfo<>(page.getResult());
    }

    @Override
    public void cancelFollow(Long id) {
        userFollowDao.cancelFollow(id);
    }
}
